package com.text.controller;

import com.github.pagehelper.PageInfo;
import com.text.entity.Category;
import com.text.entity.Goods;
import com.text.service.CategoryService;
import com.text.service.GoodsService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.responses.ApiResponse;
import io.swagger.v3.oas.annotations.responses.ApiResponses;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
@Tag(name = "CategoryCtrl",description = "商品类别控制器")
@Controller
@RequestMapping("/admin")
public class CategoryCtrl {

    @Resource
    CategoryService categoryService;



//    @RequestMapping("/goodsList")
//    public String getGoodsList(Model model){
//        ArrayList<Goods> goodsList = goodsService.getGoodsList();
//        model.addAttribute("goodsList",goodsList);
//        return "goodsList";
//    }

    @RequestMapping(value = "/category",method = {RequestMethod.GET,RequestMethod.POST})
    public String getGoodsListPage(@RequestParam(value = "pageNum",required = false,defaultValue = "1") Integer pageNum,
                                   @RequestParam(value = "pageSize",required = false,defaultValue = "6") Integer pageSize,
                                   Model model){
        PageInfo<Category> categoryListPage = categoryService.queryAllByPage(pageNum,pageSize);
        model.addAttribute("categoryListPage",categoryListPage);
        return "categoryList";
    }

//    @GetMapping("/delCategory/{id}")
//    public String delGoods(@PathVariable("id") int id, Model model) {
//        int flag =  categoryService.deleteById(id);
//        if (flag > 0){
//            model.addAttribute("msg","删除成功！");
//        } else{
//            model.addAttribute("msg","该商品不允许删除！");
//        }
//        return "forward:/admin/goodsList";
//    }
@Operation(description = "根据类别ID删除类别")
@ApiResponses(value = {@ApiResponse(description = "删除操作受影响的行数")})

    @ResponseBody
    @GetMapping("/delCategory/{id}")
    public int delGoods2(@Parameter(required = true,description = "类别ID") @PathVariable("id") int id, Model model){
        int flag=categoryService.deleteById(id);
        return flag;
    }

    @GetMapping("/gotoAddCategory")
    public String gotoAdd(Model model){
//        ArrayList<Category> categoryList = categoryService.queryAll();
//        model.addAttribute("categoryList",categoryList);
        return "categoryAdd";
    }

    @PostMapping("/addCategory")
    public String addCategory(Category category,Model model){
        int num = categoryService.insert(category);
        if (num > 0){
            model.addAttribute("msg",category.getName()+"添加成功！");
        } else{
            model.addAttribute("msg","添加失败！");
        }
        return "forward:/admin/category";
    }
    @GetMapping("toEdit/{id}")
    public String gotoEdit(@PathVariable("id") int id,Model model){
        Category edit = categoryService.edit(id);
        model.addAttribute("edit",edit);
        return "categoryEdit";
    }

    @PostMapping("/editCategory")
    public String editGoods(Category edit,Model model){
        System.out.println(edit);
        int num= categoryService.update(edit);
              if(num > 0){
      model.addAttribute("msg","商品【"+edit.getName()+"】修改成功！");
        }else{
            model.addAttribute("msg","商品【"+edit.getName()+"】修改失败！");
        }
        return "forward:/admin/category";
    }
}
